from lxml import etree
from mathutils import Matrix

# function to check if a collada file is valid
# for now if it wil well formatted
# it will also remove namespaces
def check_collada(xml_path):
  
  # check for errors
  try:
    xml = etree.parse(xml_path)
  except:
    return None
  
  # function to remove namespaces
  def strip_namespace(element):
    if element.tag.startswith("{"):
        element.tag = element.tag.split("}")[1]
    for child in element:
        strip_namespace(child)
    return element
  
  # return the clean xml element
  strip_namespace(xml.getroot())
  return xml
  
# function to return the matrix contained in a matrix element
def get_text_mat4x4(text, offset):

  # remove the starting whitespace characters
  while  (text[0] in " \n\t\r"):
    text = text[1:]
  text = text.split()
  a = []
  for num in text:
    a.append(float(num))
  for i in range(0, offset):
    a.pop(0)
  mat = Matrix(([a[0],  a[1],  a[2],  a[3]],
                [a[4],  a[5],  a[6],  a[7]],
                [a[8],  a[9],  a[10], a[11]],
                [a[12], a[13], a[14], a[15]]))
  return mat
